Skip to content

Various fixes for manual_is_power_of_two #14463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 14, 2025

Conversation

samueltardieu
Copy link
Contributor

Fix #14461:

  • insert parentheses as required in suggestion
  • check MSRV before suggesting fix in const context
  • do not lint macro expansion result

Commits have been logically separated to facilitate review, and start with a refactoring (and simplification) of the existing code.

changelog: [manual_is_power_of_two]: insert parentheses as required in suggestion, check MSRV before suggesting fix in const context, do not lint macro expansion results

@rustbot
Copy link
Collaborator

rustbot commented Mar 24, 2025

r? @blyxyas

rustbot has assigned @blyxyas.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 24, 2025
@samueltardieu samueltardieu force-pushed the push-yqozmwtrqtqs branch 2 times, most recently from d0d7a23 to abde06c Compare March 25, 2025 17:23
@samueltardieu
Copy link
Contributor Author

r? clippy

@rustbot rustbot assigned Jarcho and unassigned blyxyas Apr 6, 2025
This is a refactoring of the existing code to remove
repetitive code.
Since the fixed expression is used as a receiver for
`.is_power_of_two()`, it may require parentheses.
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great improvements to the lint! I've marked some slight nits

);
/// Return the unsigned integer receiver of `.count_ones()`
fn count_ones_receiver<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
if let ExprKind::MethodCall(method_name, receiver, [], _) = expr.kind
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also check for <int>::count_ones(x), it's a normal ExprKind::Call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Done, and tested, in a new commit on top of the others.

if let ExprKind::Binary(op, lhs, rhs) = smaller.kind
&& !lhs.span.from_expansion()
&& !rhs.span.from_expansion()
&& op.node == BinOpKind::Sub
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this op.node == BinOpKind::Sub to above the expansion checking?

Also, thinking about checking for expansion so many times, we could probably minimize some of these (as we check for expansions 3 times before linting)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! All checks are necessary, but I've factored them in through a helper function. This makes code easier to read. The modified commit is Do not lint result from macro expansion.

If parts of the expression comes from macro expansion, it may match an
expression equivalent to `is_power_of_two()` by chance only.
Copy link
Member

@blyxyas blyxyas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! ❤️ 🌈

@blyxyas blyxyas added this pull request to the merge queue Apr 14, 2025
Merged via the queue into rust-lang:master with commit 02764f6 Apr 14, 2025
11 checks passed
@samueltardieu samueltardieu deleted the push-yqozmwtrqtqs branch April 15, 2025 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Several problems with clippy::manual_is_power_of_two
4 participants